home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / noweb / contrib / kostas / inw.nw (.txt) < prev    next >
LaTeX Document  |  1995-02-24  |  6KB  |  107 lines

  1. %-*- lang : icon -*-
  2. \documentstyle [noweb,11pt,fullpage] {article}
  3. \pagestyle {noweb}
  4. \title {Extending Noweb With Some Typesetting (Icon version)}
  5. \author {Kostas N. Oikonomou \\ {\tt ko@surya.ho.att.com}}
  6. \begin {document}
  7. \maketitle
  8. \section {Introduction}
  9. This is a {\tt noweb} filter, written in Icon, which adds to {\tt noweave} the capability
  10. of some simple pretty-printing (no indentation or line-breaking!) in code sections.  This
  11. particular version implements pretty-printing for the Icon language. However, the filter is written in a way that should make it easy for someone to
  12. change the target language: most of the code is language-independent (in \S\ref{lipp} and
  13. on), while the language-dependent code occupies only sections 1 and 2.  In fact, what
  14. needs to be changed when the language changes is only the [[translation]] table in
  15. procedure [[main]], and the definitions of the ``interesting'' tokens in the beginning of
  16. \S\ref{int}\footnote{Well, hopefully, anyway.}.
  17. Using this two-part scheme, the language-independent part (which is in file [[lipp.nw]])
  18. has been used with the language-dependent files [[tnw.nw]], [[inw.nw]], and [[mnw.nw]] to 
  19. implement pretty-printing for the languages Object-Oriented Turing, Icon, and {\sl
  20. Mathematica}.
  21. <<*>>=
  22. <<Procedure [[main]]>>
  23. <<Procedure [[filter]]>>
  24. <<Procedure [[TeXify]]>>
  25. <<Global declarations>>
  26. \section {A Typesetting Facility}
  27. \subsection {The philosophy}
  28. The addition to {\tt noweb} described here is based on the following two premises
  29. \begin {itemize}
  30.   \item It should be as independent of the target language as possible, and
  31.   \item We don't want to write a full-blown scanner for the target language.
  32. \end {itemize}
  33. Strings of characters of the target language which we want to typeset specially are called
  34. ``interesting tokens''. Having had some experience with Web and SpiderWeb, we define three
  35. categories of interesting tokens:
  36. \begin {enumerate}
  37.   \item Reserved words of the target language: we want to typeset them in bold, say.
  38.   \item Other strings that we want to typeset specially: e.g. $\le$ for [[<=]].
  39.   \item Comment and quoting characters: we want what follows them or what is enclosed by
  40.     them to be typeset literally.
  41. \end {enumerate}
  42. There is a table [[translation]] which defines a translation into \TeX\ code for every
  43. interesting token in the target language. Here is an excerpt from the translation table
  44. for Icon:
  45. \begin {center}
  46. \begin {tabular}{l}
  47.   [[translation["by"] := "{\\Cb{}by}"]] \\
  48.   [[translation["break"] := "{\\Cb{}break}"]] \\
  49.   [[translation["&ascii"] := "{\\Cb{}&ascii}"]] \\
  50.   [[translation["&clock"] := "{\\Cb{}\&clock}"]] \\
  51.   [[translation[">="] := "$\\ge$"]] \\
  52.   [[translation["~="] := "$\\neq$"]]
  53. \end {tabular}
  54. \end {center}
  55. (Here the control sequence \verb+\Cb+ selects the Courier bold font\footnote{The empty group
  56. \{\} serves to separate the control sequence from its argument without introducing an
  57. extra space.}.) We use four sets of strings to define the tokens in categories 2 and 3:
  58. \begin {center}
  59.   [[special]], [[comment1]], [[comment2]], [[quote]].
  60. \end {center}
  61. [[comment1]] is for unbalanced comment strings (e.g.\ the character [[#]] in Icon),
  62. [[comment2]] is for balanced comment strings (none in Icon), and [[quote]] is for literal
  63. quotes ([["]] and [[']] in Icon), which we assume to be balanced.
  64. Our approach to recognizing the interesting tokens while scanning a line, is to have a set
  65. of characters [[begin_token]] (an Icon cset), containing all the characters by which an
  66. interesting token may begin. [[begin_token]] is the union of
  67. \begin {itemize}
  68.   \item the cset defining the characters which may begin a reserved word, and
  69.   \item the cset containing the initial characters of all strings in the special, comment,
  70.     and quote sets.
  71. \end {itemize}
  72. Given a line of text, we scan up to a character in [[begin_token]], and, depending on what
  73. this character is, we may try to complete the token by further scanning. If we succeed, we
  74. look up the token in the [[translation]] table, and if the token is found, we output its
  75. translation, otherwise we output the token itself unchanged. When comment or quote tokens
  76. are recognized, further processing of the line may stop altogether, or temporarily, until
  77. a matching token is found.
  78. <<Procedure [[main]]>>=
  79. procedure main (args)
  80.   <<The [[translation]] table>>
  81.   <<Definition of interesting tokens>>
  82.   <<Emit special {\TeX} definitions>>
  83.   <<Read and filter all the input lines>>
  84. \subsection {Definitions of the interesting tokens}
  85. \label {int}
  86. The set of characters allowed in an Icon identifier, of which a reserved word is a special
  87. case:
  88. <<Definition of interesting tokens>>=
  89. res_word_chars := &letters ++ '&$'
  90. id_chars := res_word_chars ++ &digits
  91. @ Unbalanced and balanced comment tokens, and quoting tokens. Note that [[comment2]] is a
  92. set of {\it pairs} (``open comment'', ``close comment''), while we assume that quoting
  93. tokens are s.t. the ``open quote'' and ``close quote'' tokens are identical.
  94. <<Definition of interesting tokens>>=
  95. comment1 := set(["#"])
  96. comment2 := set([])
  97. quote := set(["\"", "\'"])
  98. @ The ``special'' tokens. [[S]] is the set of all characters appearing in strings in
  99. [[special]].
  100. <<Definition of interesting tokens>>=
  101. special := set(["{", "}", "\\", "||", "<", ">", ">=", "<=", "=>", "~=", 
  102.                 "++", "**", "--"])
  103. S := ''
  104. every S := S ++ !special # Nice!
  105. The rest of the code is language-independent, and is in the file {\tt lipp.nw}
  106. (Language-Independent Pretty-Printing).
  107.